<template>
    <div style="background-color: rgb(250,250,250,1);height: 100%;">
        <topNavbar :showLeft="shouldShowLeft"
                   :showOverlay="shouldLocalOverlay"
                   :active="shouldLocalActive"
                   :localShowTitle = "localShowTitle"
                   @update:showLeft="handleShowLeftUpdate"
                   @update:localOverlay="handleLocalOverlayUpdate"
                   @update:localActive="handleLocalActiveUpdate"
                   style="margin-bottom: 10px"/>
        <!--      关注-->
        <follow v-if="shouldLocalActive==='0'"/>

        <van-pull-refresh v-model="loading" @refresh="onRefresh">
            <!--    发现-->
            <van-grid v-if="shouldLocalActive==='1'" :border="false" :column-num="2" :gutter="5">
                <van-grid-item v-for="value in 20" :key="value" class="custom-grid-item">
                    <van-image
                            src="/src/assets/logo/icon.jpg"
                    />
                    <div class="grid-info">
                        <p class="grid-info-title">
                            技术保真技术保技术保真技术保真技术保真技术保真真技术保真技术保真</p>
                        <div class="grid-info-user">

                            <div style="display: flex;">
                                <van-image round
                                           width="20px"
                                           height="20px"
                                           src="https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg"/>
                                <p class="grid-info-user-name">技术保真技术保真技术保真技术保真</p>
                            </div>
                            <div @click="sumLike">
                                <van-icon style="color: #a1a1a1" name="like-o"/>
                                <span style="color: #b2b2b2">{{ showNum }}</span>
                            </div>
                        </div>

                    </div>
                </van-grid-item>
            </van-grid>
        </van-pull-refresh>
        <!--      附近-->
        <nearby v-if="shouldLocalActive==='2'"/>

        <!--底部导航-->
        <bottomNav :active="active" @clickLoadIndex="clickLoadIndex"/>
    </div>
</template>

<script setup>
import {onMounted, ref, watch} from 'vue'
import {showToast} from "vant";

import BottomNav from "@/components/navigation/bottomNav.vue";
import TopNavbar from "@/components/top/topNavbar.vue";
import Nearby from "@/components/index/nearby.vue";
import Follow from "@/components/index/follow.vue";

const active = ref(0)
const num = ref(10999)
const showNum = ref(num.value)


watch(num, (newVal, oldValue) => {
    let c = (newVal / 10000).toFixed(1);
    if (c >= 1) {
        showNum.value = c + '万'
    } else {
        showNum.value = newVal
    }

})

const shouldShowLeft = ref(false); // 控制侧边栏显示的状态
const shouldLocalOverlay = ref(false); // 控制侧边栏显示的状态
const shouldLocalActive = ref('1') // 切换内容
const localShowTitle = ref(0) // 切换内容

const handleShowLeftUpdate = (newValue) => {
    shouldShowLeft.value = newValue; // 更新父组件中的状态
};
const handleLocalOverlayUpdate = (newValue) => {
    shouldLocalOverlay.value = newValue; // 更新父组件中的状态
    shouldShowLeft.value = false
};


const handleLocalActiveUpdate = (newValue) => {
    shouldLocalActive.value = newValue
    console.log(shouldLocalActive.value)
}

const sumLike = () => {
    num.value = ++num.value
}


/*下拉刷新*/
const loading = ref(false);
const onRefresh = () => {
    loading.value=true
    setTimeout(() => {
        showToast('刷新成功');
        loading.value = false;
    }, 1000);
}

const clickLoadIndex = ()=>{
    window.scrollTo(0, 0); // 瞬移到页面顶部
    onRefresh()
}

onMounted(()=>{
    clickLoadIndex()
})






</script>

<style>

.grid-info {
    text-align: left;
    width: 100%;
    font-size: 15px;
    margin: 5px 0 5px 0;
}

.grid-info-title {
    max-width: 15em;
    margin-top: 5px;
    word-break: break-all;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2; /* 这里是超出几行省略 */
    overflow: hidden;
    padding: 0 5px 0 5px;

}

.grid-info-user {
    display: flex; /*显示一行*/
    align-items: center;
    justify-content: space-between; /*左右显示*/
    font-size: 12px;
    padding: 0 5px 0 5px;
    margin-top: 5px;
}

.grid-info-user-name {
    font-size: 12px; /* 调整为你想要的大小 */
    margin-left: 3px;
    max-width: 100px; /* 或者你需要的任何宽度 */
    white-space: nowrap; /* 防止文本换行 */
    overflow: hidden; /* 隐藏超出容器的内容 */
    text-overflow: ellipsis; /* 用省略号表示被截断的文本 */
    font-weight: normal;
    color: #b2b2b2;
}

#app {
    max-width: 1280px;
    margin: 0 auto;

    text-align: center;
}

.van-grid-item__content--center {
    align-items: center;
    justify-content: center;
    border-radius: 5px;
}

/*用户展示*/
.van-image {
    position: relative;
    display: inline-block;
    height: 200px;

}

.van-image__img, .van-image__error, .van-image__loading {
    display: block;
    width: 100%;
    height: 100%;
    border-radius: 5px 5px 0 0; /*调整图片左上角 右上角为圆角*/
}

.van-grid-item__content {
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    height: 100%;
    padding: 0;
    background: var(--van-grid-item-content-background);
}
</style>